package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductStockMapper productStockMapper;

    //分页查询
    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum, Integer pageSize, ProductVO productVO) {
        PageHelper.startPage(pageNum,pageSize);
        ProductConverter productConverter = new ProductConverter();
        Example example = new Example(Product.class);
        String name = productVO.getName();
        Integer status = productVO.getStatus();
        Long[] categorys = productVO.getCategorys();

        Example.Criteria criteria = example.createCriteria();
        if (name != null && !"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if (categorys != null && !"".equals(categorys)){
            criteria.andEqualTo("threeCategoryId",categorys[2]);
        }
        criteria.andEqualTo("status",status);
        List<Product> products = productMapper.selectByExample(example);
        List<ProductVO> productVOS = productConverter.converterToProductVOList(products);
        PageInfo<Product> info = new PageInfo<>(products);

        return new PageVO<>(info.getTotal(),productVOS);
    }
    //新增
    @Override
    public Object add(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        product.setStatus(2);
        product.setOneCategoryId(productVO.getCategoryKeys()[0]);
        product.setTwoCategoryId(productVO.getCategoryKeys()[1]);
        product.setThreeCategoryId(productVO.getCategoryKeys()[2]);

        return productMapper.insert(product);
    }
    //修改回显
    @Override
    public Object edit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);

        return productVO;
    }
    //修改确认
    @Override
    public Object update(Long id,ProductVO productVO) {
        Product product = productMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(productVO,product);
        product.setModifiedTime(new Date());
        return productMapper.updateByPrimaryKey(product);
    }
    //移入回收站
    @Override
    public Object remove(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setModifiedTime(new Date());
        product.setStatus(1);
        return productMapper.updateByPrimaryKey(product);
    }
    //回收站撤销
    @Override
    public Object back(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setModifiedTime(new Date());
        product.setStatus(0);
        return productMapper.updateByPrimaryKey(product);
    }
    //回收站删除
    @Override
    public Object delete(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);

        return productMapper.deleteByPrimaryKey(product);
    }
    //审批通过
    @Override
    public Object publish(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setModifiedTime(new Date());
        product.setStatus(0);
        return productMapper.updateByPrimaryKey(product);
    }

    @Override
    public PageVO<ProductStockVO> findProductStocks(Integer pageSize, Integer pageNum, String categorys, String name) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        ProductVO productVO = new ProductVO();
        if (categorys != "" && categorys != null) {
            String[] split = categorys.split(",");
            if (split.length == 3) {
                productVO.setThreeCategoryId(Long.parseLong(split[2]));
            }
            if (split.length == 2) {
                productVO.setTwoCategoryId(Long.parseLong(split[1]));
            }
            if (split.length == 1) {
                productVO.setOneCategoryId(Long.parseLong(split[0]));
            }
        }
        if (name != null && !"".equals(name)) {
            criteria.andLike("name","%"+ name+"%");
        }
        if (productVO.getOneCategoryId() != null && !"".equals(productVO.getOneCategoryId())) {
            criteria.andEqualTo("oneCategoryId", productVO.getOneCategoryId());
        }
        if (productVO.getTwoCategoryId() != null && !"".equals(productVO.getTwoCategoryId())) {
            criteria.andEqualTo("twoCategoryId", productVO.getTwoCategoryId());
        }
        if (productVO.getThreeCategoryId() != null && !"".equals(productVO.getThreeCategoryId())) {
            criteria.andEqualTo("threeCategoryId", productVO.getThreeCategoryId());
        }
        List<Product> products = productMapper.selectByExample(example);
        List<ProductVO> p = new ArrayList<>();
        for (Product product : products) {
            ProductVO productVO1 = new ProductVO();
            BeanUtils.copyProperties(product, productVO1);
            p.add(productVO1);
        }
        List<ProductStockVO> productStockVOList = new ArrayList<>();
        BeanUtils.copyProperties(p,productStockVOList);
        PageInfo<Product> info = new PageInfo<>(products);

        return new PageVO<>(info.getTotal(),productStockVOList);
    }




}
